package org.zt.java.dal.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;


/**
 * Dal配置加载器，可加载本地配置和远程配置
 * @author 周涛
 *
 */
public class DalConfigLoader {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public DalConfig load(String address) {
		if (address.startsWith("file:")) {
			// 本地加载配置文件
			String localConfigPath = address.substring("file:".length());
			logger.info("load dal config:{}",localConfigPath);
			return loadByLocalConfig(localConfigPath);
		} else {
			throw new IllegalArgumentException("动态数据源address地址不合法。请用[file:|remote:]开头");
		}
	}

	private DalConfig loadByLocalConfig(String configFilePath) {
		DalConfigXmlParser dalConfigParser = new DalConfigXmlParser();
		File configFile = null;
		try {
			configFile = ResourceUtils.getFile(configFilePath);
			return dalConfigParser.parse(new FileInputStream(configFile));
		} catch (FileNotFoundException ex) {
			throw new IllegalArgumentException(
					"Dal config file does not exist! the filePath is "
							+ configFile.getAbsolutePath());
		}

	}

	@SuppressWarnings("unused")
	private DalConfig loadByRemoteConfig(String remoteAddr) {
		return null;
	}


}
